package com.xydtech.ecard.config.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.config.ServerConfig;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.config.domain.EcardServeModuleNews;
import com.xydtech.ecard.config.service.IEcardServeModuleNewsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 服务模块内部新闻Controller
 *
 * @author JHON
 * @date 2022-06-23
 */
@Controller
@RequestMapping("/ecard/config/serveModuleNews")
public class EcardServeModuleNewsController extends BaseController {
    private String prefix = "ecard/config/serveModuleNews";

    @Autowired
    private IEcardServeModuleNewsService ecardServeModuleNewsService;
    @Autowired
    private ServerConfig serverConfig;

    @RequiresPermissions("ecard:config:serveModuleNews:view")
    @GetMapping("/{functionId}")
    public String serveModuleNews(@PathVariable("functionId") Long functionId, ModelMap mmap) {
        mmap.put("functionId", functionId);
        return prefix + "/serveModuleNews";
    }

    /**
     * 查询服务模块内部新闻列表
     */
    @RequiresPermissions("ecard:config:serveModuleNews:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardServeModuleNews ecardServeModuleNews) {
        startPage();
        List<EcardServeModuleNews> list = ecardServeModuleNewsService.selectEcardServeModuleNewsList(ecardServeModuleNews);
        return getDataTable(list);
    }

    /**
     * 导出服务模块内部新闻列表
     */
    @RequiresPermissions("ecard:config:serveModuleNews:export")
    @Log(title = "服务模块内部新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardServeModuleNews ecardServeModuleNews) {
        List<EcardServeModuleNews> list = ecardServeModuleNewsService.selectEcardServeModuleNewsList(ecardServeModuleNews);
        ExcelUtil<EcardServeModuleNews> util = new ExcelUtil<EcardServeModuleNews>(EcardServeModuleNews.class);
        return util.exportExcel(list, "服务模块内部新闻数据");
    }

    /**
     * 新增服务模块内部新闻
     */
    @GetMapping("/add/{functionId}")
    public String add(@PathVariable("functionId") Long functionId, ModelMap mmap) {
        mmap.put("functionId", functionId);
        return prefix + "/add";
    }

    /**
     * 新增保存服务模块内部新闻
     */
    @RequiresPermissions("ecard:config:serveModuleNews:add")
    @Log(title = "服务模块内部新闻", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardServeModuleNews ecardServeModuleNews, HttpServletRequest request) {
        //富文本summerNote内容图片路径保存，ContextPath替换处理（ContextPath配置后缀使用）
        ecardServeModuleNews.setNewsContent(serverConfig.replaceContextPath(request, ecardServeModuleNews.getNewsContent()));
        return toAjax(ecardServeModuleNewsService.insertEcardServeModuleNews(ecardServeModuleNews));
    }

    /**
     * 修改服务模块内部新闻
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, HttpServletRequest request, ModelMap mmap) {
        EcardServeModuleNews ecardServeModuleNews = ecardServeModuleNewsService.selectEcardServeModuleNewsById(id);
        //富文本summerNote内容图片显示，ContextPath替换处理（ContextPath配置后缀使用）
        String contextPath = request.getServletContext().getContextPath();
        if (contextPath.length() > 1 && ecardServeModuleNews.getNewsContent() != null) {
            ecardServeModuleNews.setNewsContent(ecardServeModuleNews.getNewsContent().replaceAll("/profile", contextPath + "/profile"));
        }
        mmap.put("ecardServeModuleNews", ecardServeModuleNews);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务模块内部新闻
     */
    @RequiresPermissions("ecard:config:serveModuleNews:edit")
    @Log(title = "服务模块内部新闻", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardServeModuleNews ecardServeModuleNews, HttpServletRequest request) {
        //富文本summerNote内容图片路径保存，ContextPath替换处理（ContextPath配置后缀使用）
        ecardServeModuleNews.setNewsContent(serverConfig.replaceContextPath(request, ecardServeModuleNews.getNewsContent()));
        return toAjax(ecardServeModuleNewsService.updateEcardServeModuleNews(ecardServeModuleNews));
    }

    /**
     * 删除服务模块内部新闻
     */
    @RequiresPermissions("ecard:config:serveModuleNews:remove")
    @Log(title = "服务模块内部新闻", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ecardServeModuleNewsService.deleteEcardServeModuleNewsByIds(ids));
    }
}
